<link rel="stylesheet" href="module/dtree/dtree.css">
<link rel="stylesheet" href="module/dtree/font/dtreefont.css">
<div class="layui-tab layui-tab-brief" lay-filter="LAY-popup-role-permission">
    <script type="text/html" template lay-done="layui.data.sendParams(d.params)"></script>
    <ul class="layui-tab-title">
        <li class="layui-this">菜单权限</li>
        <li>数据权限</li>
    </ul>
    <div class="layui-tab-content" style="height: 100%;">
        <div class="layui-tab-item layui-show">
            <ul id="menuTree"></ul>
        </div>
        <div class="layui-tab-item">
            <ul id="permissionTree"></ul>
        </div>
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <button class="layui-btn layui-btn-warm" lay-submit lay-filter="LAY-sys-role-permission-submit" >确认授权</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>

    layui.data.sendParams = function (params) {

        layui.use(['admin', 'form', 'element', 'dtree', 'setter'], function () {
            var $ = layui.$
                , admin = layui.admin
                , form = layui.form
                , dtree = layui.dtree
                , setter = layui.setter
                , element = layui.element;

            var apiUrl = setter.apiUrl;
            /**
             * http://www.wisdomelon.com/DTreeHelper/
             */
            var hearders = {
                'Content-Type': 'application/json;charset=UTF-8',
                'token': layui.data(setter.tableName).token
            };

            dtree.render({
                elem: "#menuTree",
                url: apiUrl + '/mapi/menu/list/auth/' + params.id,
                method: 'get',
                headers: hearders,
                initLevel: 1,
                icon: "-1",  // 隐藏二级图标
                skin: "layui",
                checkbar: true,
                checkbarType: "all" // 默认就是all，其他的值为： no-all  p-casc   self  only
            });
            dtree.render({
                elem: "#permissionTree",
                url: apiUrl + '/mapi/permission/list/auth/' + params.id,
                method: 'get',
                headers: hearders,
                initLevel: 1,
                icon: "-1",  // 隐藏二级图标
                skin: "layui",
                checkbar: true,
                checkbarType: "all" // 默认就是all，其他的值为： no-all  p-casc   self  only
            });

            form.on('submit(LAY-sys-role-permission-submit)', function () {
                var menuParams = dtree.getCheckbarNodesParam("menuTree");
                //菜单父级及子菜单都要返回
                var menus = new Array();
                if (menuParams) {
                    for (var i = 0; i < menuParams.length; i++) {
                        menus.push(menuParams[i].nodeId);
                    }
                }
                var permissionParams = dtree.getCheckbarNodesParam("permissionTree");
                //权限只保留子权限，父级为菜单模块
                var permissions = new Array();
                if (permissionParams) {
                    for (var i = 0; i < permissionParams.length; i++) {
                        var single = permissionParams[i];
                        if (single.parentId !== "0") {
                            permissions.push(single.nodeId);
                        }
                    }
                }
                //http://localhost:8480/scheduling/mapi/permission/r/auth/1
                admin.req({
                    url: apiUrl + '/mapi/permission/r/auth/' + params.id
                    , method: 'put'
                    , data: JSON.stringify({"menuIds": menus, "permissionIds": permissions})
                    , done: function (response) {
                        if (response.code === 0) {
                            layer.msg('保存成功', {
                                offset: '15px', icon: 1, time: 1000
                            }, function () {
                                layer.closeAll('page');
                            });
                        } else {
                            layer.msg(response.msg, {
                                offset: '15px', icon: 2
                            });
                        }
                    }
                });
            });

        });
    }
</script>